package com.demo.schedule.regular;

import com.alibaba.fastjson.JSON;
import com.demo.entity.Patient;
import com.demo.entity.VtePreventive;
import com.demo.enums.MessageRemindEnum;
import com.demo.enums.MessageSourceEnum;
import com.demo.enums.PatientStatusEnum;
import com.demo.enums.PreventiveRiskRankEnum;
import com.demo.mapper.PatientMapper;
import com.demo.mapper.VtePreventiveMapper;
import com.demo.service.IMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;

import java.util.HashMap;
import java.util.Map;

/**
 * vte定期提醒线程
 */
@Slf4j
public class RegularVteMindThread extends Thread {

    private IMessageService messageService;
    private PatientMapper patientMapper;
    private VtePreventiveMapper vtePreventiveMapper;

    private Patient patient;
    private VtePreventive preventive;

    public RegularVteMindThread(Patient patient, VtePreventive preventive, ApplicationContext applicationContext) {
        this.patient = patient;
        this.preventive = preventive;
        this.patientMapper = applicationContext.getBean(PatientMapper.class);
        this.vtePreventiveMapper = applicationContext.getBean(VtePreventiveMapper.class);
        this.messageService = applicationContext.getBean(IMessageService.class);
    }

    @Override
    public void run() {
        log.info("vte定期提醒");
        log.info("vte定期提醒 patient = {}", JSON.toJSONString(patient));
        log.info("vte定期提醒 preventive = {}", JSON.toJSONString(preventive));
        try {
            if (!check(patient, preventive)) {
                return;
            }
            Map<String, Object> msgContent = new HashMap<>();
            msgContent.put("bed_id", patient.getBedId());
            msgContent.put("bed_name", patient.getBedName());
            msgContent.put("patient_name", patient.getPatientName());
            msgContent.put("grade", preventive.getPreventiveRiskRank());
            msgContent.put("grade_name", PreventiveRiskRankEnum.getByValue(preventive.getPreventiveRiskRank()).getDesc());
            msgContent.put("dept_name", patient.getDeptName());
            msgContent.put("dept_sn", patient.getDeptSn());

            msgContent.put("message_text", "病人上次评估结果为<span style=\"color:red\">" + PreventiveRiskRankEnum.getByValue(preventive.getPreventiveRiskRank()).getDesc() + "</span>,请进行动态评估");
            messageService.sendMessageNotification(MessageRemindEnum.PING_GU_REMIND, patient, MessageSourceEnum.OTHER, msgContent, 0);

            Thread.sleep(2 * 60 * 1000);//睡眠 2 分钟
            RegularMindSchedule.addVteTask(preventive);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 发送消息提醒前置检查
     * @return
     */
    public boolean check(Patient patient, VtePreventive preventive) {
        // 1.病人状态
        patient = patientMapper.queryValidPatient(patient.getPatientId());
        if (patient == null || patient.getStatus() == PatientStatusEnum.CHU_YUAN.getValue()) {
            log.info("当前病人已出院，不在继续进行提醒操作!");
            return false;
        }

        // 2.最新定期提醒评估和当前 preventive 是否是同一个
        VtePreventive recentPreventive = vtePreventiveMapper.queryRecentVtePreventiveByPatientId(patient.getPatientId());
        if (recentPreventive == null) {
            log.info("vte评估记录不存在");
            return false;
        }
        if (recentPreventive.getVtePreventiveId().intValue() > preventive.getVtePreventiveId().intValue()) {
            log.info("已经有比当前vte更新的定期提醒评估，不在继续进行提醒操作!");
            return false;
        }
        preventive = recentPreventive;
        return true;
    }
}
